package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.UserInfoOtherActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.RunData;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;

import java.util.List;

/**
 */
public class LikeMembersAdapter extends GzBaseAdapter{
    private List<User> datas = null;
    private Context context;
    private GlideUtils glideUtils;
    public LikeMembersAdapter(Context context) {
        this.context = context;
        glideUtils = new GlideUtils();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public LikeMembersAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_like_plepeo,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final LikeMembersAdapter.ViewHolder viewHolder= (ViewHolder) holder;
        final User mUser=datas.get(position);
        glideUtils.Glidde_loadCropCircleImage(context,mUser.getLogo(),viewHolder.mUser_icon);
        viewHolder.name.setText(mUser.getNick());
        viewHolder.mUser_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, UserInfoOtherActivity.class);
                intent.putExtra("user",mUser);
                context.startActivity(intent);
            }
        });
        viewHolder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, UserInfoOtherActivity.class);
                intent.putExtra("user",mUser);
                context.startActivity(intent);
            }
        });
        if(null!=mUser.getUserAuth()){
            if ("-1".equals(mUser.getUserAuth().getType())){
                viewHolder.vip.setVisibility(View.GONE);
            }
            if("0".equals(mUser.getUserAuth().getType())){
                viewHolder.vip.setVisibility(View.VISIBLE);
                viewHolder.vip.setImageResource(R.drawable.v_huan);
            }else if("1".equals(mUser.getUserAuth().getType())){
                viewHolder.vip.setVisibility(View.VISIBLE);
                viewHolder.vip.setImageResource(R.drawable.v_lan);
            }
        }else{
            viewHolder.vip.setVisibility(View.GONE);
        }


        if(mUser.getInteractState()==0){
            viewHolder.mPull_text.setText("关注");
            viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);
            viewHolder.mPull_icon.setVisibility(View.GONE);
            viewHolder.mPull_text.setTextColor(Color.parseColor("#fb4746"));
        }else if(mUser.getInteractState()==10){
            viewHolder.mPull_text.setText("已关注");
            viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector1);
            viewHolder.mPull_icon.setVisibility(View.VISIBLE);
            viewHolder.mPull_text.setTextColor(Color.parseColor("#999999"));
        }else if(mUser.getInteractState()==3){
            viewHolder.mPull_text.setText("相互关注");
            viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector1);
            viewHolder.mPull_icon.setVisibility(View.VISIBLE);
            viewHolder.mPull_text.setTextColor(Color.parseColor("#999999"));
        }
        if(mUser.getId().equals(GuZhiApplication.getInstance().getUserId())){
            viewHolder.mPull_layout.setVisibility(View.GONE);
        }else{
            viewHolder.mPull_layout.setVisibility(View.VISIBLE);
        }
        viewHolder.mPull_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mUser.getInteractState()==0){
                    subscribe(mUser,viewHolder,true);
                }else{
                    subscribe(mUser,viewHolder,false);
                }
            }
        });

        if ("".equals(mUser.getIntro())) {
            viewHolder.peoplejs.setText("警察蜀黍，就是这个人不写签名");
        }else{
            viewHolder.peoplejs.setText(mUser.getIntro());
        }

    }


    //获取数据的数量
    @Override
    public int getItemCount() {
//        return 5;
        return datas.size();
    }

//    public List<User> getDatas() {
//        return datas;
//    }

    public void setDatas(List<User> datas) {
        this.datas = datas;
    }




    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView mUser_icon , vip;
        private TextView name;
        private TextView peoplejs;
        private LinearLayout mPull_layout;
        private TextView mPull_text;
        private ImageView mPull_icon;
        private RelativeLayout root;
        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener){
            super(view, listener, longClickListener);
            mUser_icon = (ImageView) view.findViewById(R.id.user_icon);
            vip = (ImageView) view.findViewById(R.id.vip);
            name = (TextView) view.findViewById(R.id.name);
            peoplejs = (TextView) view.findViewById(R.id.peoplejs);
            mPull_layout = (LinearLayout) view.findViewById(R.id.pull_layout);
            mPull_text = (TextView) view.findViewById(R.id.pull_text);
            mPull_icon = (ImageView) view.findViewById(R.id.pull_icon);
            root = (RelativeLayout) view.findViewById(R.id.root);
        }
    }
    private void subscribe(User userBean, ViewHolder holder, boolean res){
        MessageParam messageParam=new MessageParam();
        messageParam.setUserId(GuZhiApplication.getInstance().getUserId());
        messageParam.setToUserId(userBean.getId());
        if(res) {
            messageParam.setTarget("add");
            new FriendApi(context).getSubscribe(messageParam, new ResultClass(userBean, holder));
        }else {
            messageParam.setTarget("delete");
            new FriendApi(context).getSubscribe(messageParam, new ResultClass(userBean, holder));
        }
    }

    class ResultClass implements VolleyListener {
        User userBean;
        ViewHolder viewHolder;
        public ResultClass(User userBean,ViewHolder holder){
            this.userBean=userBean;
            this.viewHolder=holder;
        }

        @Override
        public void onStart(String tag) {
            showDialog(context,"正在请求...",false);
        }

        @Override
        public void onSuccess(String response, String tag) {
            dismissDialog();
            if (JSONHelper.isSuccess(response)) {
                RunData data = JsonUtil.getMode(response,
                        RunData.class);


                if (tag.equals(ApiConfig.APIURL.URL_SUBSCRIBE)) {
                    userBean.setInteractState(Integer.parseInt(data.getData()));
                    if("0".equals(data.getData())){
                        viewHolder.mPull_text.setText("关注");
                        viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector);
                        viewHolder.mPull_icon.setVisibility(View.GONE);
                        viewHolder.mPull_text.setTextColor(Color.parseColor("#fb4746"));
                    }else if("10".equals(data.getData())){
                        viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector1);
                        viewHolder.mPull_icon.setVisibility(View.VISIBLE);
                        viewHolder.mPull_text.setTextColor(Color.parseColor("#999999"));
                        viewHolder.mPull_text.setText("已关注");
                    }else if("3".equals(data.getData())){
                        viewHolder.mPull_layout.setBackgroundResource(R.drawable.shape_gz_bq_red_selector1);
                        viewHolder.mPull_icon.setVisibility(View.VISIBLE);
                        viewHolder.mPull_text.setTextColor(Color.parseColor("#999999"));
                        viewHolder.mPull_text.setText("相互关注");
                    }else {
                        StringUtils.postToast(context,data.getData()+"");
                    }

                }
            }

        }

        @Override
        public void onFaile(VolleyError volleyError, String tag) {
            dismissDialog();
            ResultCode.toastVolleyError(context, volleyError);
        }
    }
}
